package com.demoku.web.web.demoAdmin;

import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iff.infra.util.JsonHelper;
import org.iff.infra.util.mybatis.plugin.Page;
import org.iff.infra.util.mybatis.plugin.PageVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demoku.web.application.demoAdmin.ClassTypeApplication;
import com.demoku.web.application.demoAdmin.vo.ClassTypeVO;
import com.demoku.web.web.common.BaseController;

@Controller
public class ClassTypeController extends BaseController {

	@Inject
	ClassTypeApplication classTypeApplication;

	@RequestMapping("/classTypeIndex")
	public String index() {
		return "index";
	}
	
	@RequestMapping("/toAddClassType.do")
	public String toAddClassType(HttpServletRequest request, HttpServletResponse response) {
		return "demoAdmin/classTypeAdmin/addClassType";
	}

	@RequestMapping("/addClassType.do")
	public String addClassType(ClassTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		classTypeApplication.saveClassType(vo);
		
		modelMap.addAttribute("message", "录入成功！");
		modelMap.addAttribute("toPage", "/pageFindAllClassType.do");
		return "common/auth_messages";
	}
	
	@RequestMapping("/deleteClassType.do")
	public String deleteClassType(String id, ClassTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		classTypeApplication.removeClassType(id);
		modelMap.addAttribute("message", "删除成功！");
		modelMap.addAttribute("toPage", "/pageFindAllClassType.do");
		
		return "common/auth_messages";
	}
	
	@RequestMapping("/getClassTypeInfo.do")
	public String getAuthUserInfo(String id, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		modelMap.addAttribute("classType", classTypeApplication.getClassTypeVO(id));
		return "demoAdmin/classTypeAdmin/infoClassType";
	}
	
	@RequestMapping("/toEditClassType.do")
	public String toEditClassType(String id, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		modelMap.addAttribute("classType", classTypeApplication.getClassTypeVO(id));
		return "demoAdmin/classTypeAdmin/editClassType";
	}
	
	@RequestMapping("/editClassType.do")
	public String editClassType(ClassTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		classTypeApplication.saveClassType(vo);
		modelMap.addAttribute("message", "编辑成功！");
		modelMap.addAttribute("toPage", "/pageFindAllClassType.do");
		return "common/auth_messages";
	}

	@RequestMapping("/pageFindAllClassType.do")
	public String pageFindAllAccount(ClassTypeVO vo, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		
		int currentPage = 0;
		if(request.getParameter("currentPage") != null)
			currentPage = Integer.valueOf(request.getParameter("currentPage"));
		
		Page pages = Page.pageable(10, currentPage, 0, null);
		Page page = classTypeApplication.pageFindAllClassType(vo, pages);
		modelMap.addAttribute("page", page);
		modelMap.addAttribute("classType", vo);
		
		return "demoAdmin/classTypeAdmin/pageFindAllClassType";
	}
		
}
